/**
 * 详情对话框
 */
var MdzxInfoDlg = {
    data: {
        btyBrandId: "",
        btyBrandIdView: "",
        mdBianma: "",
        mdZhujima: "",
        btyArchivesId: "",
        btyArchivesIdView: "",
        fzrDianhua: "",
        mianji: "",
        yujiTouzi: "",
        yusuanHeji: "",
        chengbenHeji: "",
        touzishengyu: "",
        status: "",
        statusView: "",
        delFlag: "",
        createUser: "",
        createUserName: "",
        createTime: "",
        updateUser: "",
        updateUserName: "",
        updateTime: "",
        company: "",
        companyView: "",
        srcType: "",
        srcValue: "",
        qy: "",
        qyName: ""
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    form.verify({
        double: function (value) {
            if(value!=""){
                var patt2=/^(\-|\+)?\d+(\.\d+)?$/;
                if (patt2.test(value) == false) {
                    return '你输入的不是数字';
                }
            }

            // if (/^\d+$/.test(value) == false && /^\d+\.\d+$/.test(value) == false) {
            //     return '你输入的不是数字';
            // }
        }
    });

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/dgdmdzx/detail?btyZxMdzxId=" + Feng.getUrlParam("btyZxMdzxId"));
    var result = ajax.start();
    form.val('mdzxForm', result.data);

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        if(data.field.qyName==""||$("#qyName").val()==""){
            Feng.error("请选择区域");
            return false;
        }
        var ajax = new $ax(Feng.ctxPath + "/dgdmdzx/editItem", function (data) {
            if(data.code==200){
                Feng.success("更新成功！");
                // window.location.href = Feng.ctxPath + '/mdzx'
                closePage();
            }else {
                Feng.error(data.message);
            }


        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });

        if(data.field.btyBrandId==""&&$("#btyBrandIdView").val()==""){
            Feng.error("请选择门店或者填写门店名称");
            return false;
        }
        if(data.field.btyBrandId==""){
            data.field.btyBrandId=0;
        }
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    function closePage(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
    //返回按钮
    $("#backupPage").click(function () {
        // window.location.href = Feng.ctxPath + '/mdzx'
        closePage();
    });


    /**
     * 初始化门店
     */
    var languageAjax = new $ax(Feng.ctxPath + "/project/allProjectList?useType=edit");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        $("#btyBrandId").append("<option value='" + val.id + "'>" + val.name + "</option>");
    });
    $('#btyBrandId').val(result.data.btyBrandId);
    form.render('select');
    //门店改变时，负责人联动，为建立工程时对应的开发人员
    form.on('select(btyBrandId)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/project/selectOneByParam?id=" + data.value);
        var result = ajax.start();
        if (result != null && result != "" && result.data != null && result.data != "" && result.data.createUserName != null && result.data.createUserName != "") {
            $("#btyArchivesIdView").val(result.data.createUserName);
        }
    });

    $('#qyName').click(function () {
        var formName = encodeURIComponent("parent.MdzxInfoDlg.data.qyName");
        var formId = encodeURIComponent("parent.MdzxInfoDlg.data.qy");
        var treeUrl = encodeURIComponent("/area/tree");

        layer.open({
            type: 2,
            title: '选择区域',
            area: ['300px', '360px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#qy").val(MdzxInfoDlg.data.qy);
                $("#qyName").val(MdzxInfoDlg.data.qyName);
            }
        });
    });

});